package cn.com.yang.config;

import java.util.HashSet;
import java.util.Set;

import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.RedisNode.RedisNodeBuilder;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import redis.clients.jedis.JedisPoolConfig;

/**
 * 在springboot中集中两个redis TODO
 * 
 * @author yangyijin
 * @since 2023年11月3日-上午9:23:22
 */
@Configuration
public class RedisTwoConfig extends CachingConfigurerSupport {
	private static final String host2 = "127.0.0.1";
	private static final String port = "6379";
	private static final String db_index = "0";
	private static final String password = "123456";

	private int pollMaxActive = 100;
	private int pollMaxIdle = 50;
	private int pollMinIdle = 10;
	private int pollMaxWait = 30;
	private int timeout = 10000;

//	@Bean
//	public GenericObjectPoolConfig genericObjectPoolConfig() {
//		GenericObjectPoolConfig config = new GenericObjectPoolConfig();
//		config.setMaxIdle(pollMaxIdle);
//		config.setMaxTotal(pollMaxActive);
//		config.setMaxWaitMillis(pollMaxWait);
//		config.setMinIdle(pollMinIdle);
//		return config;
//	}
	/**
	 * 配置第二个redis实例
	 * 
	 * @return
	 */
	@Bean(name="redisMockTemplate")
	public RedisTemplate<String, Object> redisMockTemplate() {
		RedisTemplate<String, Object> redisTemplate = this.getRedisConnectionFactory(); // 建立Redis的连接
		return redisTemplate;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public RedisTemplate<String, Object> getRedisConnectionFactory() {
		JedisPoolConfig poolConfig = new JedisPoolConfig(); // 进行连接池配置
		poolConfig.setMaxTotal(pollMaxActive);
		poolConfig.setMaxIdle(pollMaxIdle);
		poolConfig.setMinIdle(pollMinIdle);
		poolConfig.setMaxWaitMillis(pollMaxWait);
		
		RedisClusterConfiguration cluserConfig = new RedisClusterConfiguration();
		RedisNode node1 = new RedisNodeBuilder().listeningAt("127.0.0.1", 6379).build();
		RedisNode node2 = new RedisNodeBuilder().listeningAt("127.0.0.1", 6479).build();
		RedisNode node3 = new RedisNodeBuilder().listeningAt("127.0.0.1", 6579).build();
		Set<RedisNode> cluserNodes = new HashSet();
		cluserNodes.add(node1);
		cluserNodes.add(node2);
		cluserNodes.add(node3);
		cluserConfig.setClusterNodes(cluserNodes);
		// spring-data-redis-2.3.1
		RedisPassword pwd = RedisPassword.of("123456");
		cluserConfig.setPassword(pwd);
		
		JedisConnectionFactory jedisFactory = new JedisConnectionFactory(cluserConfig, poolConfig);
		jedisFactory.afterPropertiesSet(); // 初始化连接池配置
		RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
		redisTemplate.setKeySerializer(new StringRedisSerializer());
		redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
		redisTemplate.setHashKeySerializer(new StringRedisSerializer());
		redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
		redisTemplate.setConnectionFactory(jedisFactory);
		return redisTemplate;
	}
}
